package cn.stronglink.assetmanage;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.stronglink.assetmanage.adapter.MenuTitleAdapter;
import cn.stronglink.assetmanage.business.check.checkorder.CheckActivity;
import cn.stronglink.assetmanage.business.inventory.view.InventoryActivity;
import cn.stronglink.assetmanage.business.login.LoginActivity;
import cn.stronglink.assetmanage.business.patrol.view.PatrolActivity;
import cn.stronglink.assetmanage.business.repair.order.RepairOrderActivity;
import cn.stronglink.assetmanage.business.search.SearchActivity;
import cn.stronglink.assetmanage.business.setting.SettingActivity;
import cn.stronglink.assetmanage.business.sync.OneKeySyncActivity;
import cn.stronglink.assetmanage.business.task.TaskActivity;
import cn.stronglink.assetmanage.entity.TaskEntity;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.listener.MenuRecyclerListener;
import cn.stronglink.assetmanage.utils.PrefUtils;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.netrequest.business.user.LoginService;
import cn.stronglink.netrequest.util.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BaseActivity extends FragmentActivity implements MenuRecyclerListener {

    /**
     * 左边栏标题列表
     */
    @BindView(R.id.rv_business)
    RecyclerView rvBusinessList;
    /**
     * 业务标题头
     */
    @BindView(R.id.tv_business_name)
    protected TextView tvBusinessName;
    /**
     * 父Activity和子Activity的布局都add本view上，所有Activity的最外层布局都需要是ConstraintLayout
     */
    private LinearLayout parentLayout;
    /**
     * 子Activity需要放置的布局
     */
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.fl_user_info)
    LinearLayout flUserInfo;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    private List<TaskEntity> menuDatas = new ArrayList<>();
    private MenuTitleAdapter mMenuTitleAdapter;
    LoginService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView(R.layout.activity_base);
        ButterKnife.bind(this);
        initViewAndData();
    }

    private void initViewAndData() {
        initLeftMenu();
        initMenuData();
        initUserInfo();
        service = new LoginService();
    }

    @OnClick(R.id.fl_user_info)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_user_info:
                //退出登录
                logout();
                break;
            default:
                break;
        }
    }


    private void logout() {
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                try {
                    service.loginOut(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } catch (Exception e) {

                    System.out.println(e.getMessage());
                }
            }
        });
        ab.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });
        ab.setMessage("确定要退出登录吗?");
        ab.setTitle("提示");
        ab.show();
    }

    private void initLeftMenu() {
        TaskEntity entity = new TaskEntity();
        entity.setTaskPromission(0);
        entity.setTaskResuorces(R.drawable.task_press);
        entity.setTaskText(getString(R.string.menu_my_task));
        menuDatas.add(entity);

        TaskEntity entity2 = new TaskEntity();
        entity2.setTaskPromission(1);
        entity2.setTaskResuorces(R.drawable.checkincoming_press);
        entity2.setTaskText(getString(R.string.menu_check));
        menuDatas.add(entity2);

        TaskEntity entity3 = new TaskEntity();
        entity3.setTaskPromission(2);
        entity3.setTaskResuorces(R.drawable.inventory);
        entity3.setTaskText(getString(R.string.menu_inventory));
        menuDatas.add(entity3);

        TaskEntity entity4 = new TaskEntity();
        entity4.setTaskPromission(3);
        entity4.setTaskResuorces(R.drawable.inspection_press);
        entity4.setTaskText(getString(R.string.menu_patrol));
        menuDatas.add(entity4);

        TaskEntity entity5 = new TaskEntity();
        entity5.setTaskPromission(4);
        entity5.setTaskResuorces(R.drawable.inspection_press);
        entity5.setTaskText(getString(R.string.menu_repair));
        menuDatas.add(entity5);

        TaskEntity entity6 = new TaskEntity();
        entity6.setTaskPromission(5);
        entity6.setTaskResuorces(R.drawable.onekeysync);
        entity6.setTaskText(getString(R.string.menu_sync_data));
        menuDatas.add(entity6);

        TaskEntity entity7 = new TaskEntity();
        entity7.setTaskPromission(6);
        entity7.setTaskResuorces(R.drawable.jserach);
        entity7.setTaskText(getString(R.string.menu_search));
        menuDatas.add(entity7);

        TaskEntity entity8 = new TaskEntity();
        entity8.setTaskPromission(7);
        entity8.setTaskResuorces(R.drawable.systing);
        entity8.setTaskText(getString(R.string.menu_setting));
        menuDatas.add(entity8);
    }

    private void initMenuData() {
        mMenuTitleAdapter = new MenuTitleAdapter(this);
        mMenuTitleAdapter.setOnItemClickListener(this);
        rvBusinessList.addItemDecoration(new DividerItemDecoration(getApplicationContext(),
                DividerItemDecoration.VERTICAL));
        rvBusinessList.setLayoutManager(new LinearLayoutManager(getApplicationContext(),
                LinearLayoutManager.VERTICAL, false));
        rvBusinessList.setAdapter(mMenuTitleAdapter);
        mMenuTitleAdapter.notifyDatasChanged(menuDatas);
    }

    private void initUserInfo() {
        tvUserName.setText(User.getInstance().getName());
    }

    private void initContentView(int layoutResID) {
        //得到窗口的根布局
        ViewGroup clContent = findViewById(android.R.id.content);
        //首先移除在根布局上的组件
        clContent.removeAllViews();
        //创建自定义父布局
        parentLayout = new LinearLayout(this);
        clContent.addView(parentLayout);
        //将父类Activity布局添加到父布局上
        LayoutInflater.from(this).inflate(layoutResID, parentLayout, true);
    }

    @Override
    public void setContentView(int layoutResID) {
        //将子类Activity布局添加到父类Activity布局上
        LayoutInflater.from(this).inflate(layoutResID, llContent, true);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        //将子类Activity布局添加到父类Activity布局上
        llContent.addView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        //将子类Activity布局添加到父类Activity布局上
        llContent.addView(view, params);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onMenuItemClicked(View v, int postion) {
        jumpActivity(postion);
    }


    private void jumpActivity(int postion) {
        mMenuTitleAdapter.setSelectPosition(postion);
        switch (postion) {
            case 0:
                //我的任务
                jumpActivity(TaskActivity.class);
                break;
            case 1:
                //入库
                jumpActivity(CheckActivity.class);
                break;
            case 2:
                //盘点
                jumpActivity(InventoryActivity.class);
                break;
            case 3:
                //巡检管理
                jumpActivity(PatrolActivity.class);
                break;
            case 4:
                //维修登记
                jumpActivity(RepairOrderActivity.class);
                break;
            case 5:
                //数据同步
                jumpActivity(OneKeySyncActivity.class);
                break;
            case 6:
                //资产查询
                jumpActivity(SearchActivity.class);
                break;
            case 7:
                //系统设置
                jumpActivity(SettingActivity.class);
                break;
            default:
                break;
        }
        ToastUtil.cancelShow();
        HttpUtil.cancelAllReuest();
        finish();
    }

    private void jumpActivity(Class activityClass) {
        startActivity(new Intent(this, activityClass));
        overridePendingTransition(0, 0);
        finish();
    }

    protected void setSelectPosition(int position) {
        if (mMenuTitleAdapter != null) {
            mMenuTitleAdapter.setSelectPosition(position);
        }
        showMenuName(position);
    }

    private void showMenuName(int position) {
        rvBusinessList.getLayoutManager().scrollToPosition(position);
        switch (position) {
            case 0:
                //我的任务
                tvBusinessName.setText(getString(R.string.menu_my_task));
                break;
            case 1:
                //入库
                tvBusinessName.setText(getString(R.string.menu_check));
                break;
            case 2:
                //盘点
                tvBusinessName.setText(getString(R.string.menu_inventory));
                break;
            case 3:
                //巡检管理
                tvBusinessName.setText(getString(R.string.menu_patrol));
                break;
            case 4:
                //维修登记
                tvBusinessName.setText(getString(R.string.menu_repair));
                break;
            case 5:
                //数据同步
                tvBusinessName.setText(getString(R.string.menu_sync_data));
                break;
            case 6:
                //资产查询
                tvBusinessName.setText(getString(R.string.menu_search));
                break;
            case 7:
                //系统设置
                tvBusinessName.setText(getString(R.string.menu_setting));
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
